package com.yzt.zhuju.dialog1;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.yzt.zhuju.R;
import com.yzt.zhuju.utils.ScreenUtils;
import com.weigan.loopview.LoopView;

import java.util.ArrayList;
import java.util.List;


/**
 * <pre>
 *      author : TK
 *      time : 2017/7/31
 *      desc : 一级滚轮
 * </pre>
 */
@SuppressLint("NonConstantResourceId")
public class BaseSelectDialog<T> extends BaseDialog {


    LoopView loopView;

    private OnSelectListener<T> onSelectListener;

    private final Transformer<T> transformer;

    private final List<T> mList;

    private final T indexData;

    public BaseSelectDialog(Context context, List<T> list, Transformer<T> transformer, int index) {
        this(context, list, transformer, list.get(index));
    }

    public BaseSelectDialog(Context context, List<T> list, Transformer<T> transformer, T indexData) {
        super(context);
        this.mList = list;
        this.transformer = transformer;
        this.indexData = indexData;
    }

    @Override
    protected int layoutId() {
        return R.layout.dialog_select_picker_layout;
    }

    @Override
    protected void onContentViewSet() {
        loopView = findViewById(R.id.loop_view);
        getWindow().getAttributes().width = ScreenUtils.getScreenWidth();
        getWindow().setGravity(Gravity.BOTTOM);
        getWindow().setWindowAnimations(R.style.BottomDialog);
        List<String> strList = new ArrayList<>();
        int index = 0;
        for (int i = 0; i < mList.size(); i++) {
            strList.add(transformer.transform(mList.get(i)));
            if (mList.get(i).equals(indexData)) {
                index = i;
            }
        }
        loopView.setNotLoop();
        loopView.setCenterTextColor(ContextCompat.getColor(getContext(),R.color.dark_303030));
        loopView.setOuterTextColor(ContextCompat.getColor(getContext(),R.color.dark_bdbdbd));
        loopView.setTextSize(15);
        loopView.setItemsVisibleCount(7);
        loopView.setItems(strList);
        loopView.setInitPosition(index);

        findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (null != onSelectListener) {
                    onSelectListener.onSelect(mList.get(loopView.getSelectedItem()),
                            loopView.getSelectedItem());
                }
            }
        });

        findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void setOnSelectListener(OnSelectListener<T> onSelectListener) {
        this.onSelectListener = onSelectListener;
    }

    public interface OnSelectListener<T> {
        void onSelect(T data, int index);
    }

    public interface Transformer<T> {
        String transform(T data);
    }
}
